1771A - Hossam and Combinatorics - CodeForces Solution


math math

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    mini_count = l.count(min(l))
    maxi_count = l.count(max(l))
    ins = 0
    if max(l) == min(l):
        print(mini_count * (mini_count - 1))
    else:
        print(maxi_count * mini_count * 2)

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
class DSU

{

    public :

    vector<int> parent , size;

    DSU(int n)

    {
        for(int i = 0 ; i <= n ; i++)

        {
            parent.push_back(i);
            size.push_back(1);
        }
    }

    int findPar(int node)

    {

        if(parent[node] == node)
        return node;

        return parent[node] = findPar(parent[node]);

    }

    

    void Union(int a , int b)
    {

        int parA = findPar(a);
        int parB = findPar(b);

        if(parA == parB)
        return ;
        if(size[parA] < size[parB])

        {
            size[parB] += size[parA];
            parent[parA] = parB;

        }
        else

        {
            size[parA] += size[parB];
           parent[parB] = parA;
        }
    }

};

int power(int x, int y, int p)
{
 
    // Initialize answer
    int res = 1;
 
    // Check till the number becomes zero
    while (y > 0) {
 
        // If y is odd, multiply x with result
        if (y % 2 == 1)
            res = (res * x);
 
        // y = y/2
        y = y >> 1;
 
        // Change x to x^2
        x = (x * x);
    }
    return res % p;
}


void solve()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 for(int i=0;i<n;i++)

 cin>>a[i];
 sort(a,a+n);

 if(a[0]==a[n-1])
 {
    cout<<n*(n-1)<<endl;
    return;
 }
 map<int,int>m;
 int maxi=*max_element(a,a+n);
 int mini=*min_element(a,a+n);
 for(int i=0;i<n;i++)
 {
   m[a[i]]++; 
}
  
 cout<<m[maxi]*m[mini]*2<<endl;


}



   

                                 
main()                                                                            
{
    ios_base::sync_with_stdio(false);                         
    cin.tie(NULL);                          
    #ifndef ONLINE_JUDGE                 
    freopen("input.txt", "r", stdin);                                                
    freopen("output.txt", "w", stdout);                    
    #endif         
    int test_cases=1;                       
    cin>>test_cases;

    for(int i=1;i<=test_cases;i++)
    { 
     solve();
    }
    cout<<fixed<<setprecision(10);
    cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n"; 
} 


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship